import java.awt.Point;
import java.util.HashSet;

public class Level {

	/**
	 * Main
	 * @param args
	 */
	public static void main(String[] args) {
		Level.start();
	}
	
	public static void start() {		
		loadLevel();
		//printLevel();
		
		for (Move startNode : startNodes) {
			MarioGraph.addNode(startNode);
		}
		
		MarioGraph.printWinner();
	}
	
	static public int rows;
	static public int colums;
	static public Character[][] grid;
	static private Point startPoint;
	static private HashSet<Move> startNodes = new HashSet<Move>();
	
	/**
	 * Load level
	 */
	public static void loadLevel() {
		rows = Integer.parseInt(StdIn.readLine());
		colums = Integer.parseInt(StdIn.readLine());
		grid = new Character[rows][colums];
		
		for(int i = 0; i < rows; i++) {
			String line = StdIn.readLine();		
			for(int j=0; j < colums; j++) {
				Character c = line.charAt(j);
				grid[i][j] = c;
				//Set startPoints
				if (c.equals('S')) {	
					startNodes.add(new Move(new Point(j,i),new Point(0,0),1));
				}	
			}
		}
	}

	/**
	 * Print level
	 */
	private static void printLevel() {
		for(int i = 0; i < rows; i++) {	
			for(int j=0; j < colums; j++) {
				System.out.print(grid[i][j]);
			}
			System.out.println();
		}
		
		System.out.println("StartPoint(" + startPoint.x + "," + startPoint.y +")");
	}
}
